import com.alibaba.fastjson.JSON;
import com.redislabs.modules.rejson.JReJSON;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.core.parameters.P;
import redis.clients.jedis.*;
import redis.clients.jedis.json.JsonProtocol;
import redis.clients.jedis.json.Path;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.providers.PooledConnectionProvider;
import redis.clients.jedis.search.*;

import java.io.Serializable;
import java.util.Map;

public class RedisJsonDemo2 {

    public static void main(String[] args) {
        String input = "I like Java,jAva is very easy and jaVa is so popular.";
        String replacement="cccc";

        System.out.println(input);
        System.out.println(input.replaceAll("(?i)java", replacement));
    }

    private UnifiedJedis jedis;

    //redis-cli -h redis-18465.c276.us-east-1-2.ec2.cloud.redislabs.com -p 18465 -a FfksNXhIiT1Gz47oLrGSOASFBZIOB4CT
    @Before
    public void unifiedJedis() {
//        HostAndPort config = new HostAndPort("192.168.133.217", 6380);
        HostAndPort config = new HostAndPort("redis-18465.c276.us-east-1-2.ec2.cloud.redislabs.com", 18465);
        PooledConnectionProvider provider = new PooledConnectionProvider(config);
        jedis = new UnifiedJedis(provider);
        jedis.sendCommand(Protocol.Command.AUTH, new String[]{"FfksNXhIiT1Gz47oLrGSOASFBZIOB4CT"});
    }

    @Test
    public void testUser(){
        User user = new User();
        user.setUsername("中华人民共和国");
        user.setAge(25);

        jedis.jsonSetWithEscape("user:lry", new Path2("$"),user);

        User u = jedis.jsonGet("user:lry",User.class);
        System.out.println(u);
    }

    @Test
    public void search(){
        SearchResult searchResult = jedis.ftSearch("userIdx", new Query("@\\$\\.username:中华人民共和国").highlightFields("$.username").returnFields("$.username").setLanguage("english"));
        print(searchResult);
    }

    private void print(SearchResult searchResult){
        System.out.println("total result num="+searchResult.getTotalResults());
        for (Document document : searchResult.getDocuments()) {
            Iterable<Map.Entry<String, Object>> it = document.getProperties();
            it.forEach(entry->
                System.out.println("key="+entry.getKey()+" , value=" +entry.getValue())
            );
        }
    }

    @Test
    public void createIndex( ){
        //FT.CREATE {index_name} ON JSON SCHEMA {json_path} AS {attribute} {type}
        //FT.CREATE userIdx ON JSON SCHEMA $.user.username AS username TEXT $.user.age AS age TAG
        //FT.SEARCH userIdx '@name:(John)'
        try{
            jedis.ftDropIndex("userIdx");
        }catch (Exception e){
        }

        Schema schema = new Schema().addTextField("$.username",1.0).addNumericField("$.age");

        IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON);

        jedis.ftCreate("userIdx", IndexOptions.defaultOptions().setDefinition(rule), schema);
    }

    @Test
    public void testUserStr(){
        User user = new User();
        user.setUsername("刘仁有");
        user.setAge(25);
        jedis.jsonSet("user:lry", new Path("."),JSON.toJSONString(user));

        String u = (String) jedis.jsonGet("user:lry",new Path("NOESCAPE"));
        System.out.println(u);
    }




    @Data
    class User implements Serializable{
        private String username;
        private Integer age;
    }
}
